package org.edu.grade.controller;

import org.edu.grade.entity.User;
import org.edu.grade.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class userController {

    @Autowired
    private UserService userService;

    @PostMapping("/test")
    public  List<User> test() {
        User user = userService.getById(13510093755l);
        System.out.println(user.toString());
        List<User> list = userService.list();
        System.out.println(user.getUserName());
        return list;
    }
    @PostMapping("/test1")
    public  String test1() {
        return "123123";
    }

    @PostMapping("/vaildUser")
    public String vaildUser(@ModelAttribute("username") String username, Model model, ModelMap map) {
        // 处理表单数据
//        model.addAttribute("message", "Hello, " + username + "!");
//        System.out.println(model.getAttribute("message"));
        User user = userService.getById(username);
        map.put("message", "Hello, " + user.getUserName() + "!");
        return "grade";
    }

    @GetMapping("/pageB")
    public String getPageB(@RequestParam(name = "param", required = true, defaultValue = "北京大学") String param,@RequestParam(name = "schoolCode", required = true, defaultValue = "11") String schoolCode, ModelMap map,Model model) {
        System.out.println(param);
        map.put("schoolName",param.toString());
        map.put("schoolCode",schoolCode.toString());
        return "singleSchool"; // 返回pageB.html
    }


    @GetMapping("/tosearchPage")
    public String tosearchPage(@RequestParam(name = "schoolName", required = true, defaultValue = "北京大学") String param,@RequestParam(name = "schoolCode", required = true, defaultValue = "10001") String schoolCode, ModelMap map,Model model) {
        System.out.println(schoolCode);
        map.put("schoolName",param.toString());
        map.put("schoolCode",schoolCode.toString());
        return "searchPage"; // 返回pageB.html
    }
}
